using System;
using System.Data;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.CMS.Components;
using ASPNETVN.PORTAL.Components.Share.Controls;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.CMS.Controls
{
    public partial class Item2 : ModulePage
    {
        PagedDataSource pds;
        protected Boolean ShowDetail = true;
        protected Unit WidthImage;

        protected string GetFileUrl(int id, string url)
        {
            if (this.Settings["StorageType"].ToString().ToUpper().Equals("DATABASE"))
            {
                return HttpUrlBuilder.BuildUrl(this.ModuleID, id, "Download");
            }
            return url;
        }

        private void BuildPageLink()
        {
            if (Request.QueryString["CategoryID"] != null)
            {
                int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");
                Category category = Category.Load(categoryID);
                if (category != null)
                {
                    Category parentCategory = Category.Load(category.ParentID);
                    if (parentCategory != null)
                    {
                        pagelink.AddLink(parentCategory.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", parentCategory.ID)));

                        string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID));
                        pagelink.AddLink(category.Title, url);
                    }
                    else
                    {
                        pagelink.AddLink(category.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID)));
                    }
                }
            }
        }

        private void GridDataBinding()
        {
            DataView dv;
            if (Request.QueryString["CategoryID"] != null)
            {
                dv = Components.Item.GetItemsByCategoryID(Convert.ToInt32(Request.QueryString["CategoryID"])).Tables[0].DefaultView;
            }
            else
            {
                if (Request.QueryString["OptionView"] != null)
                {
                    dv = Components.Item.GetTopItems(this.ModuleID, Request.QueryString["OptionView"]).Tables[0].DefaultView;
                }
                else
                {
                    dv = Components.Item.GetTopItems(this.ModuleID, string.Empty).Tables[0].DefaultView;
                }
            }

            pds.DataSource = dv;
            DataListItem.DataSource = pds;
            DataListItem.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int CountColumn = int.Parse(this.Settings["Columns.Count"].ToString());
            DataListItem.RepeatColumns = CountColumn;
            DataListItem.ItemStyle.Width =  new Unit((100 / CountColumn) + "%");
            ShowDetail = Boolean.Parse(this.Settings["ShowDetail"].ToString());
            WidthImage = new Unit(this.Settings["Picture.Width"].ToString());

            pds = new PagedDataSource();
            pds.AllowPaging = true;
            pds.PageSize = 10;
            if (IsPostBack == false)
            {
                this.BuildPageLink();
                this.GridDataBinding();
                if (pds.PageCount > 1)
                {
                    for (int i = 0; i < pds.PageCount; i++)
                    {
                        int pageNumber = i + 1;
                        ListItem item = new ListItem(pageNumber.ToString());
                        PageDropDownList.Items.Add(item);
                        PageDropDownList.SelectedIndex = 0;
                    }
                    PageLabel.Text = " / " + pds.PageCount;
                }
                else
                {
                    MessageLabel.Visible = PageDropDownList.Visible = false;                    
                }
                pds.CurrentPageIndex = 0;
            }
        }

        protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            pds.CurrentPageIndex = PageDropDownList.SelectedIndex;
            this.GridDataBinding();            
        }        
    }
}